home *** CD-ROM | disk | FTP | other *** search
/ Programming Languages Suite / ProgramD2.iso / Borland / Borland C++ V5.02 / STDLIB.PAK / ALG4.CPP < prev    next >
C/C++ Source or Header  |  1997-05-06  |  3KB  |  92 lines

  1. /**************************************************************************
  2.  *
  3.  * alg4.cpp - Example programs for STL generic algorithms removal
  4.  *    algorithms. Section 12.5
  5.  *
  6.  * $Id: alg4.cpp,v 1.3 1995/08/29 18:43:12 oberg Exp $
  7.  *
  8.  * $$RW_INSERT_HEADER "slyrs.cpp"
  9.  *
  10.  **************************************************************************/
  11.  
  12. # include <list>
  13. # include <set>
  14. # include <algorithm>
  15.  
  16. # include <iostream.h>
  17. using namespace std;
  18.  
  19. bool isEven(int n) { return 0 == (n % 2); }
  20.  
  21. void remove_example ()
  22.     // illustrate the use of the remove algorithm
  23. {
  24.     cout << "Remove Algorithm examples" << endl;
  25.     
  26.     // create a list of numbers
  27.     int data[] = {1, 2, 4, 3, 1, 4, 2};
  28.     list<int> aList;
  29.     copy (data, data+7, inserter(aList, aList.begin()));
  30.     cout << "Original list: ";
  31.     copy (aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  32.     
  33.         // remove 2's, copy into a new list
  34.     list<int> newList;
  35.     remove_copy (aList.begin(), aList.end(), back_inserter(newList), 2);
  36.     cout << "After removing 2's: ";
  37.     copy (newList.begin(), newList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  38.     
  39.         // remove 2's in place
  40.     list<int>::iterator where;
  41.     where = remove(aList.begin(), aList.end(), 2);
  42.     cout << "List after removal, before erase: ";
  43.     copy (aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  44.     aList.erase(where, aList.end());
  45.     cout << "List after erase: ";
  46.     copy (aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  47.     
  48.         // remove all even values
  49.     where = remove_if (aList.begin(), aList.end(), isEven);
  50.     aList.erase(where, aList.end());
  51.     cout << "List after removing even values: ";
  52.     copy (aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  53.     
  54. }
  55.  
  56. void unique_example ()
  57.     // illustrate use of the unqiue algorithm
  58. {
  59.         // first make a list of values
  60.     int data[] = {1, 3, 3, 2, 2, 4};
  61.     list<int> aList;
  62.     copy(data, data+6, inserter(aList, aList.begin()));
  63.     cout << "Origianal List: ";
  64.     copy(aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  65.     
  66.         // copy unique elements into a set
  67.     set<int, less<int> > aSet;
  68.     unique_copy(aList.begin(), aList.end(), inserter(aSet, aSet.begin()));
  69.     cout << "Set after unique_copy: ";
  70.     copy(aSet.begin(), aSet.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  71.     
  72.         // copy unique elements in place
  73.     list<int>::iterator where;
  74.     where = unique(aList.begin(), aList.end());
  75.     cout << "List after calling unique: ";
  76.     copy(aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  77.     
  78.         // remove trailing values
  79.     aList.erase(where, aList.end());
  80.     cout << "List after erase: ";
  81.     copy(aList.begin(), aList.end(), ostream_iterator<int>(cout, " ")), cout << endl;
  82. }
  83.  
  84. int main() {
  85.     cout << "STL generic algorithms -- Removal Algorithms" << endl;
  86.     remove_example();
  87.     unique_example();
  88.     
  89.     cout << "End of removal algorithms sample program" << endl;
  90.     return 0;
  91. }
  92.